import Global from "../Global";
import GameLayer from "./GameLayer";
import UIBaseWindow from "./UIBaseWindow";
import UISystem from "./UISystem";

export default class LayerType {

	constructor() { }

	/**场景ui层 */
	public static readonly TYPE_SCENE: number = 1;
	/**主界面层 */
	public static readonly TYPE_MAIN: number = 2;
	/**窗口层 3*/
	public static readonly TYPE_WINDOW: number = 3;
	/**显示层 4 */
	public static readonly TYPE_VISIBLE: number = 4;
	/**弹窗提示层 5*/
	public static readonly TYPE_ALERT: number = 5;
	/**顶层 6 */
	public static readonly TYPE_TOP: number = 6;
	/**开放域层 7 */
	public static readonly TYPE_OPEN: number = 7;
	/**广告层 8 */
	public static readonly TYPE_AD: number = 8;

	/**左上 1*/
	public static readonly LAYOUT_LEFT_TOP: number = 1;
	/**中上 2*/
	public static readonly LAYOUT_CENTER_TOP: number = 2;
	/**右上 3*/
	public static readonly LAYOUT_RIGHT_TOP: number = 3;
	/**左中 4*/
	public static readonly LAYOUT_LEFT_CENTER: number = 4;
	/**中心 5*/
	public static readonly LAYOUT_CENTER: number = 5;
	/**右中 6*/
	public static readonly LAYOUT_RIGHT_CENTER: number = 6;
	/**左下 7*/
	public static readonly LAYOUT_LEFT_BOTTOM: number = 7;
	/**中下 8*/
	public static readonly LAYOUT_CENTER_BOTTOM: number = 8;
	/**右下 9*/
	public static readonly LAYOUT_RIGHT_BOTTOM: number = 9;


    /**
	 * 设置9宫格布局
	 */
	public static SetWindowLayout(view: UIBaseWindow): void {
		let vs = view.GetViewStruct();
		switch (vs.layout) {
			case LayerType.LAYOUT_LEFT_TOP://1
				{
					view.x = 0;
					view.y = 0;
					break;
				}
			case LayerType.LAYOUT_CENTER_TOP://2
				{
					view.x = (Global.stageWidth - view.viewWidth) * 0.5;
					view.y = 0;
					break;
				}
			case LayerType.LAYOUT_RIGHT_TOP://3
				{
					view.x = Global.stageWidth;
					view.y = 0;
					break;
				}
			case LayerType.LAYOUT_LEFT_CENTER://4
				{
					view.x = 0;
					view.y = (Global.stageHeight - view.viewHeight) * 0.5;
					break;
				}
			case LayerType.LAYOUT_CENTER://5
				{
					view.x = Math.ceil((Global.stageWidth - view.viewWidth) * 0.5);
					view.y = Math.ceil((Global.stageHeight - view.viewHeight) * 0.5);
					break;
				}
			case LayerType.LAYOUT_RIGHT_CENTER://6
				{
					view.x = Global.stageWidth;
					view.y = (Global.stageHeight - view.viewHeight) * 0.5;
					break;
				}
			case LayerType.LAYOUT_LEFT_BOTTOM://7
				{
					view.x = 0;
					view.y = Global.stageHeight;
					break;
				}
			case LayerType.LAYOUT_CENTER_BOTTOM://8
				{
					view.x = (Global.stageWidth - view.viewWidth) * 0.5;
					view.y = Global.stageHeight;
					break;
				}
			case LayerType.LAYOUT_RIGHT_BOTTOM://9
				{
					view.x = Global.stageWidth - view.viewWidth;
					view.y = Global.stageHeight;
					break;
				}
		}
	}

    /**
	 * 添加显示层级
	 */
	public static SetWindowLayer(view: UIBaseWindow): void {

		let i: number = 0;
		let len: number = 0;
		let panel: UIBaseWindow;

		let isExclusion = view.GetViewStruct().isExclusion;

		switch (view.GetViewStruct().layerType) {
			//场景界面
			case LayerType.TYPE_SCENE:
				{
					//先关闭窗口界面的所有面板
					if (isExclusion) {
						len = GameLayer.fightLayer.numChildren;
						for (i = len - 1; i >= 0; i--) {
							panel = GameLayer.fightLayer.GetChildAt(i) as UIBaseWindow;
							if (panel == view) continue;
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}

					GameLayer.fightLayer.AddChild(view);
					break;
				}
			//主界面
			case LayerType.TYPE_MAIN:
				{
					//先关闭窗口界面的所有面板
					if (isExclusion) {
						len = GameLayer.mainLayer.numChildren;
						for (i = len - 1; i >= 0; i--) {
							panel = GameLayer.mainLayer.GetChildAt(i) as UIBaseWindow;
							if (panel == view) continue;
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}

					GameLayer.mainLayer.AddChild(view);
					break;
				}

			//窗口界面
			case LayerType.TYPE_WINDOW:
				{
					if (isExclusion) {
						len = GameLayer.windowLayer.numChildren;
						for (i = len - 1; i >= 0; i--) {
							panel = GameLayer.windowLayer.GetChildAt(i) as UIBaseWindow;
							if (panel == view) continue;
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}
					GameLayer.windowLayer.AddChild(view);
					break;
				}

			//显示界面
			case LayerType.TYPE_VISIBLE:
				{
					if (isExclusion) {
						len = GameLayer.visibleLayer.numChildren;
						for (i = len - 1; i >= 0; i--) {
							panel = GameLayer.visibleLayer.GetChildAt(i) as UIBaseWindow;
							if (panel == view) continue;
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}
					GameLayer.visibleLayer.AddChild(view);
					break;
				}

			//弹框界面
			case LayerType.TYPE_ALERT:
				{
					if (isExclusion) {
						len = GameLayer.alertLayer.numChildren;
						for (i = len - 1; i >= 0; i--) {
							panel = GameLayer.alertLayer.GetChildAt(i) as UIBaseWindow;
							if( panel == view ) continue;
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}
					GameLayer.alertLayer.AddChild(view);
					break;
				}

			//顶层界面,不添加有遮罩的界面
			case LayerType.TYPE_TOP:
				if (isExclusion) {
					len = GameLayer.topLayer.numChildren;
					for (i = len - 1; i >= 0; i--) {
						panel = GameLayer.topLayer.GetChildAt(i) as UIBaseWindow;
						if( panel == view ) continue;
						if (panel instanceof UIBaseWindow) {
							if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
						}
					}
				}
				GameLayer.topLayer.AddChild(view);
				break;

			case LayerType.TYPE_OPEN:
				if (isExclusion) {
					len = GameLayer.openLayer.numChildren;
					for (i = len - 1; i >= 0; i--) {
						panel = GameLayer.openLayer.GetChildAt(i) as UIBaseWindow;
						if( panel == view ) continue;
						if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
					}
				}
				GameLayer.openLayer.AddChild(view);
				break;

			case LayerType.TYPE_AD:
				if (isExclusion) {
					len = GameLayer.adLayer.numChildren;
					for (i = len - 1; i >= 0; i--) {
						panel = GameLayer.adLayer.GetChildAt(i) as UIBaseWindow;
						if( panel == view ) continue;
						if (panel.GetViewStruct().isExclusion) UISystem.Inst.RemoveWindowView(panel);
					}
				}
				GameLayer.adLayer.AddChild(view);
				break;
		}
	}
}